<?php

namespace App\Http\Controllers\Api\V1;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

// use Illuminate\Http\Request;
// use App\Models\Authorization;
// use App\Transformers\AuthorizationTransformer;

class ColumnController extends BaseController
{
    /**
     * 获取栏目的全级数据
     * @get("{spid}/column/{columnid}/list0")
     * @Versions({"v1"})
     * @Request()
     * @Response("status": 200, "data":{},"route":{}})
     */
    public function listIndex(Request $request, $spid, $termid, $columnid)
    {
        // $termid = $request->header('device-token');
        if (empty($termid)) {
            return response()->json(['status' => '404', 'data' => '设备ID为空!']);
        }
        if (empty($columnid)) {
            return response()->json(['status' => '404', 'data' => '栏目ID不能为空!']);
        }
        $column = DB::table('columns')->where(['columnid' => $columnid])->first();
        if (empty($column)) {
            return response()->json(['status' => '404', 'data' => '没有相关栏目信息!']);
        }
        // FTP 配置
        $ftpUrl = config('app.host.ftp');

        $data['rowid']                       = $column->columnid;
        $data['img']                         = $ftpUrl . $column->img;
        $data['title']                       = $column->title;
        $data['subtitle']                    = $column->subtitle;
        $data['tags']                        = json_decode($column->tags);
        $data['onfocus']['route']['tplType'] = null;
        $data['onfocus']['route']['tplID']   = null;
        $data['onfocus']['href']             = url("/{$spid}/{$termid}/column/" . $column->columnid . "/list1");
        $data['onclick']['route']['tplType'] = null;
        $data['onclick']['route']['tplID']   = null;
        $data['onclick']['href']             = url("/{$spid}/{$termid}/column/" . $column->columnid . "/list1");
        // 根据ID查询对应栏目列表信息以及对应媒资信息
        $subcolumns = DB::table('subcolumns')->where(['columnid' => $columnid])->orderBy('listorder')->get();
        if (!empty($subcolumns)) {
            foreach ($subcolumns as $subcolumnKey => $subcolumnValue) {
                $data['data'][$subcolumnKey]['rowid']                       = $subcolumnValue->subcolumnid;
                $data['data'][$subcolumnKey]['title']                       = $subcolumnValue->title;
                $data['data'][$subcolumnKey]['subtitle']                    = $subcolumnValue->subtitle;
                $data['data'][$subcolumnKey]['img']                         = $ftpUrl . $subcolumnValue->img;
                $data['data'][$subcolumnKey]['tags']                        = json_decode($subcolumnValue->tags);
                $data['data'][$subcolumnKey]['onfocus']['route']['tplType'] = null;
                $data['data'][$subcolumnKey]['onfocus']['route']['tplID']   = null;
                $data['data'][$subcolumnKey]['onfocus']['href']             = url("/{$spid}/{$termid}/column/" . $subcolumnValue->subcolumnid . "/list1");
                $data['data'][$subcolumnKey]['onclick']['route']['tplType'] = null;
                $data['data'][$subcolumnKey]['onclick']['route']['tplID']   = null;
                $data['data'][$subcolumnKey]['onclick']['href']             = url("/{$spid}/{$termid}/column/" . $subcolumnValue->subcolumnid . "/list1");
                // 取出所有二级栏目关联的媒资
                $vassetiIds = explode('&', $subcolumnValue->vassetis);
                foreach ($vassetiIds as $key => $value) {
                    // 根据媒资ID查询对应媒资
                    $vassetis = DB::table('vassetis')->where(['vasseti_id' => $value])->first();
                    if (!empty($vassetis)) {
                        $data['data'][$subcolumnKey]['data'][$key]['rowid'] = $vassetis->vasseti_id;
                        // $data['data'][$subcolumnKey]['data'][$key]['img']      = json_decode($vassetis->imgs, 1);
                        // 图片
                        $img = json_decode($vassetis->imgs, 1);
                        if (empty($img)) {
                            $data['img'] = (object) [];
                        } else {
                            $img[0]['href']                                   = $ftpUrl . $img[0]['href'];
                            $data['data'][$subcolumnKey]['data'][$key]['img'] = $img[0];
                        }
                        $data['data'][$subcolumnKey]['data'][$key]['title']    = $vassetis->title;
                        $data['data'][$subcolumnKey]['data'][$key]['subtitle'] = $vassetis->subtitle;
                        $data['data'][$subcolumnKey]['data'][$key]['tags']     = DB::table('vasseti_vcattags')->where('vasseti_vcattags.vasseti_id', $value)->join('vcattags', 'vasseti_vcattags.vcattag_id', '=', 'vcattags.vcattag_id')->pluck('vcattags.name')->implode(',', 'name');

                        $data['data'][$subcolumnKey]['data'][$key]['onfocus']['route']['tplType'] = null;
                        $data['data'][$subcolumnKey]['data'][$key]['onfocus']['route']['tplID']   = null;
                        $data['data'][$subcolumnKey]['data'][$key]['onfocus']['href']             = url("/{$spid}/{$termid}/vasset/{$vassetis->vasseti_id}/meta");
                        $data['data'][$subcolumnKey]['data'][$key]['onclick']['route']['tplType'] = null;
                        $data['data'][$subcolumnKey]['data'][$key]['onclick']['route']['tplID']   = null;
                        $data['data'][$subcolumnKey]['data'][$key]['onclick']['href']             = url("/{$spid}/{$termid}/vasset/{$vassetis->vasseti_id}/meta");
                    }
                }
            }
        } else {
            $data['data'] = [];
        }
        return response()->json(['status' => '200', 'data' => $data]);
    }

    /**
     * 获取栏目的0级数据
     * @get("{spid}/column/{columnid}/list0")
     * @Versions({"v1"})
     * @Request()
     * @Response("status": 200, "data":{},"route":{}})
     */
    public function list0Index(Request $request, $spid, $termid, $collistid)
    {
        if (empty($termid)) {
            return response()->json(['status' => '404', 'data' => '设备ID为空!']);
        }
        if (empty($collistid)) {
            return response()->json(['status' => '404', 'data' => '栏目列表ID不能为空']);
        }

        $collist = DB::table('collists')->where(['collistid' => $collistid])->first();
        if (empty($collist)) {
            return response()->json(['status' => '404', 'data' => '无此栏目列表信息']);
        }
        // 一级栏目列表
        if ($collistid == 1) {
            // 点播
            $template_type = "VODCOLUMN";
        } elseif ($collistid = 2) {
            // 直播
            $template_type = "LIVECOLUMN";
        } elseif ($collistid == 3) {
            // 回看
            $template_type = "REVIEWCOLUMN";
        }
        $data  = [];
        $datas = [];
        // FTP 配置
        $ftpUrl = config('app.host.ftp');
        // 获取所有的栏目
        $columns = DB::table('columns')->orderBy('listorder')->get();
        foreach ($columns as $colKey => $colValue) {
            // 获取当前栏目所属的栏目列表
            $collistIds = json_decode($colValue->collistid, true);
            if ($collistIds) {
                // 检查当前栏目列表是否在栏目拥有的栏目列表中
                if (in_array($collistid, $collistIds)) {
                    $data['rowid']                       = $colValue->columnid;
                    $data['img']                         = $ftpUrl . $colValue->img;
                    $data['title']                       = $colValue->title;
                    $data['subtitle']                    = $colValue->subtitle;
                    $data['tags']                        = $colValue->tags;
                    $data['onfocus']['route']['tplType'] = $template_type;
                    $data['onfocus']['route']['tplID']   = null;
                    $data['onfocus']['href']             = url("/{$spid}/{$termid}/column/" . $colValue->columnid . "/list1");
                    $data['onclick']['route']['tplType'] = $template_type;
                    $data['onclick']['route']['tplID']   = null;
                    $data['onclick']['href']             = url("/{$spid}/{$termid}/column/" . $colValue->columnid . "/list1");
                    $datas[]                             = $data;
                }
            }
        }
        return response()->json(['status' => '200', 'data' => $datas]);
    }

    /**
     * 获取栏目的0级数据 -- 获取栏目的数据
     * @get("{spid}/column/{columnid}/list")
     * @Versions({"v1"})
     * @Request()
     * @Response("status": 200, "data":{},"route":{}})
     */
    public function list0(Request $request, $spid, $column_id)
    {
        // 指定栏目列表数据
        $collists = DB::table('collists')->where('collistid', $column_id)->first();
        // 首页
        if (empty($collists)) {
            return response()->json(['status' => '404', 'data' => '栏目列表数据为空!']);
        }
        $columns = $collists->columns;
        if (empty($columns)) {
            return response()->json(['status' => '404', 'data' => '栏目列表下无栏目信息!']);
        }
        $columns = json_decode($columns);
        // FTP 配置
        $ftpUrl = config('app.host.ftp');
        foreach ($columns as $column_key => $column_value) {
            // 根据栏目id查找对应二级栏目Id
            $column                        = DB::table('columns')->where('columnid', $column_value->id)->first();
            $data[$column_key]['rowid']    = $column_value->id;
            $data[$column_key]['title']    = $column_value->title;
            $data[$column_key]['subtitle'] = $column->subtitle;
            $data[$column_key]['img']      = $ftpUrl . $column->img;

            $data[$column_key]['tags']                        = json_decode($column->tags);
            $data[$column_key]['onfocus']['route']['tplType'] = 'AREA';
            $tplID                                            = '00' . ($column_key + 1);
            $data[$column_key]['onfocus']['route']['tplID']   = $tplID;
            $data[$column_key]['onclick']['route']['tplType'] = 'AREA';
            $tplID                                            = '00' . ($column_key + 1);
            $data[$column_key]['onclick']['route']['tplID']   = $tplID;

            $data[$column_key]['onfocus']['href'] = url("/area/template_type/area/template_no/{$tplID}");
            // 点击事件
            $data[$column_key]['onclick']['href'] = url("/area/template_type/area/template_no/{$tplID}");
        }
    }

    /**
     *  获取2级栏目数据
     * @get("{spid}/column/{columnid}/list2")
     * @Versions({"v1"})
     * @Request()
     * @Response("status": 200, "data":{},"route":{}})
     */
    public function list2Index(Request $request, $spid, $termid, $subcolumnid)
    {
        // $termid = $request->header('device-token');
        if (empty($termid)) {
            return response()->json(['status' => '404', 'data' => '设备ID为空!']);
        }
        // 获取二级栏目的媒资数据
        if (empty($subcolumnid)) {
            return response()->json(['status' => '404', 'data' => '二级栏目ID不能为空!']);
        }
        $subcolumns = DB::table('subcolumns')
            ->where('subcolumnid', $subcolumnid)
            ->first();
        if (empty($subcolumns)) {
            return response()->json(['status' => '404', 'data' => '无此二级栏目!']);
        }

        // 查看此栏目是否在区位模板编辑过
        $templateAreaData = DB::table('template_area_datas')->where(['subcolumn_id' => $subcolumnid])->groupBy('area_no')->get();
        if (empty($templateAreaData)) {
            return response()->json(['status' => '404', 'data' => '此二级栏目下无相关媒资信息!']);
        }
        $data  = [];
        $datas = [];
        $areas = [];
        // FTP 配置
        $ftpUrl = config('app.host.ftp');
        foreach ($templateAreaData as $templateAreaData_key => $templateAreaData_value) {
            // 根取出二级栏目的ID
            $vassetiIds = DB::table('template_subcolumn_vasseti_datas')
                ->where(['area_no' => $templateAreaData_value->area_no, 'subcolumn_id' => $templateAreaData_value->subcolumn_id])
                ->pluck('vasseti_id');

            foreach ($vassetiIds as $vassetiIdKey => $vassetiIdValue) {
                // 根据每一个媒资ID查询对应媒资信息
                $vasseti = DB::table('vassetis')->where(['vasseti_id' => $vassetiIdValue])->first();
                if (empty($vasseti)) {
                    return response()->json(['status' => '404', 'data' => '媒资为空!']);
                }
                $data['rowid']    = $vasseti->vasseti_id;
                $data['title']    = $vasseti->title;
                $data['subtitle'] = $vasseti->subtitle;
                // $data['img']                         = $vasseti->imgs;
                // 海报
                $posters = json_decode($vasseti->posters, 1);
                if (empty($posters)) {
                    $data['posters'] = (object) [];
                } else {
                    $posters[0]['href'] = $ftpUrl . $posters[0]['href'];
                    $data['posters']    = $posters[0];
                }

                // 图片
                $img = json_decode($vasseti->imgs, 1);
                if (empty($img)) {
                    $data['img'] = (object) [];
                } else {
                    $img[0]['href'] = $ftpUrl . $img[0]['href'];
                    $data['img']    = $img[0];
                }

                $data['onfocus']['route']['tplType'] = null;
                $data['onfocus']['route']['tplID']   = null;
                $data['onfocus']['href']             = url("/{$spid}/{$termid}/vasset/" . ($vasseti->vasseti_id) . "/meta");
                $data['onclick']['route']['tplType'] = null;
                $data['onclick']['route']['tplID']   = null;
                $data['onclick']['href']             = url("/{$spid}/{$termid}/vasset/" . ($vasseti->vasseti_id) . "/meta");
                $datas[]                             = $data;
            }
            $areas[$templateAreaData_value->area_no] = $datas;
            $datas                                   = [];
        }
        return response()->json(['status' => '200', 'data' => $areas]);
    }

    /**
     * 根据2级分类获取的栏目数据
     * @get("{spid}/column/{columnid}/list2/{catid}/{subcatid}")
     * @Versions({"v1"})
     * @Request()
     * @Response("status": 200, "data":{},"route":{}})
     */
    public function list2CatIndex(Request $request, $spid, $termid, $collist_id, $column_id, $subcolumn_id)
    {
        // $termid = $request->header('device-token');
        // 先去查询当前栏目列表的数据
        $data  = [];
        $datas = [];
        $area  = [];
        $areas = [];
        // 循环拼装以及数据
        $limit = $request->input('limit', 6);
        $page  = intval($request->input('page', 1)) ? intval($request->input('page')) : 1;
        $limit = $page > 1 ? 12 : 6;
        $skip  = $page > 1 ? (($page - 1) * $limit) - 6 : 0;
        if (empty($termid)) {
            return response()->json(['status' => '404', 'data' => '设备ID为空!']);
        }
        if (empty($collist_id)) {
            return response()->json(['status' => '404', 'content' => '栏目列表不能为空!']);
        }
        if (empty($column_id)) {
            return response()->json(['status' => '404', 'content' => '栏目ID为空!']);
        }
        if (empty($subcolumn_id)) {
            return response()->json(['status' => '404', 'content' => '二级栏目列表栏目ID为空!']);
        }
        $column = DB::table('columns')->where(['columnid' => $column_id])->first();
        if (empty($column)) {
            return response()->json(['status' => '404', 'content' => '无此栏目列表!']);
        }
        // FTP 配置
        $ftpUrl = config('app.host.ftp');
        // 拼装一级栏目数据
        $data['rowid']                       = $column->columnid;
        $data['title']                       = $column->title;
        $data['subtitle']                    = $column->subtitle;
        $data['tags']                        = $column->tags;
        $data['onfocus']['route']['tplType'] = null;
        $data['onfocus']['route']['tplID']   = null;
        $data['onfocus']['href']             = url("/{$spid}/{$termid}/column/{$column->columnid}/list2");
        $data['onclick']['route']['tplType'] = null;
        $data['onclick']['route']['tplID']   = null;
        $data['onclick']['href']             = url("/{$spid}/{$termid}/column/" . ($column->columnid) . "/list2");
        // 拼装二级栏目列表数据
        $subcolumn = DB::table('subcolumns')->where(['subcolumnid' => $subcolumn_id])->first();
        if (empty($subcolumn)) {
            $data['data']                          = [];
            $data['data']['data']['AREA005x1x1']   = [];
            $data['data']['data']['AREA005x1x0y0'] = [];
            $data['data']['data']['AREA005x1x0y1'] = [];
        } else {
            $data['data']['rowid']                       = $subcolumn->subcolumnid;
            $data['data']['title']                       = $subcolumn->title;
            $data['data']['subtitle']                    = $subcolumn->subtitle;
            $data['data']['tags']                        = $subcolumn->tags;
            $data['data']['onfocus']['route']['tplType'] = null;
            $data['data']['onfocus']['route']['tplID']   = null;
            $data['data']['onfocus']['href']             = url("/{$spid}/{$termid}/column/{$collist_id}/list2/{$column->columnid}/{$subcolumn->subcolumnid}?page=1&limit=12");
            $data['data']['onclick']['route']['tplType'] = null;
            $data['data']['onclick']['route']['tplID']   = null;
            $data['data']['onclick']['href']             = url("/{$spid}/{$termid}/column/{$collist_id}/list2/{$column->columnid}/{$subcolumn->subcolumnid}?page=1&limit=12");

            // 获取二级栏目的媒资
            $templateAreaData = DB::table('template_area_datas')
                ->select('area_no')
                ->where([
                    'template_no'   => '005',
                    'template_type' => 'area',
                    'column_id'     => $column_id,
                    'subcolumn_id'  => $subcolumn_id,
                ])
                ->groupBy('area_no')
                ->get();
            // 二级栏目下数据是否为空
            if (empty($templateAreaData->first())) {
                $data['data']['data'] = (object) [];
                //if (empty($templateAreaData)) {
                //  $data['data']['data']['AREA005x1x1']   = [];
                // $data['data']['data']['AREA005x1x0y0'] = [];
                // $data['data']['data']['AREA005x1x0y1'] = [];
            } else {
                $templateSubcolumnVassetiData = [];
                foreach ($templateAreaData as $templateAreaData_key => $templateAreaData_value) {
                    // 区位模板为AREA005x1x1时,接受参数分页
                    if ($templateAreaData_value->area_no == 'AREA005x1x1') {
                        // 获取area005x1x1大量媒资的信息
                        $templateSubcolumnVassetiData = DB::table('template_subcolumn_vasseti_datas')
                            ->where(['subcolumn_id' => $subcolumn_id, 'area_no' => $templateAreaData_value->area_no])
                            ->skip($skip)
                            ->take($limit)
                            ->select(['vasseti_id', 'type'])
                            ->get();
                        // ->pluck('vasseti_id');
                    } else {
                        $templateSubcolumnVassetiData = DB::table('template_subcolumn_vasseti_datas')
                            ->where(['subcolumn_id' => $subcolumn_id, 'area_no' => $templateAreaData_value->area_no])
                            ->select(['vasseti_id', 'type'])
                            ->get();
                        // ->pluck('vasseti_id');
                    }

                    if ($templateSubcolumnVassetiData->first()) {
                        // 根据获取的媒资Id获取相应媒资数据
                        foreach ($templateSubcolumnVassetiData as $vassetiKey => $vassetiValue) {
                            if ($vassetiValue->type == 1) {
                                $vasseti = DB::table('vassetis')->where(['vasseti_id' => $vassetiValue->vasseti_id])->first();
                                if (!empty($vasseti)) {
                                    $area['rowid']    = $vasseti->vasseti_id;
                                    $area['title']    = $vasseti->title;
                                    $area['subtitle'] = $vasseti->subtitle;
                                    // 图片
                                    if ($templateAreaData_value->area_no == 'AREA005x1x0y0') {
                                        $templateImgs = DB::table('template_area_images')->where(['area_no' => $templateAreaData_value->area_no, 'subcolumn_id' => $subcolumn_id])->first();
                                        if ($templateImgs) {
                                            $area['img'] = json_decode(json_encode(['href' => $ftpUrl . $templateImgs->img]));
                                        } else {
                                            $area['img'] = (object) [];
                                        }
                                    } else {
                                        $img = json_decode($vasseti->imgs, 1);
                                        if (empty($img)) {
                                            $area['img'] = (object) [];
                                        } else {
                                            foreach ($img as $imgKey => $imgValue) {
                                                $imgHref = image_match($imgValue['href'], 'SIMG');
                                                if ($imgHref) {
                                                    $area['img'] = json_decode(json_encode(['href' => $ftpUrl . $imgHref]));
                                                    break;
                                                } else {
                                                    $area['img'] = (object) [];
                                                }
                                            }
                                        }
                                    }
                                    $area['tags'] = DB::table('vasseti_vcattags')->where('vasseti_vcattags.vasseti_id', $vassetiValue->vasseti_id)->join('vcattags', 'vasseti_vcattags.vcattag_id', '=', 'vcattags.vcattag_id')->pluck('vcattags.name')->implode(',', 'name');

                                    $area['onfocus']['route']['tplType'] = null;
                                    $area['onfocus']['route']['tplID']   = null;
                                    $area['onfocus']['href']             = url("/{$spid}/{$termid}/vasset/{$vasseti->vasseti_id}/meta");
                                    $area['onclick']['route']['tplType'] = null;
                                    $area['onclick']['route']['tplID']   = null;
                                    $area['onclick']['href']             = url("/{$spid}/{$termid}/vasset/{$vasseti->vasseti_id}/meta");
                                } else {
                                    $area['rowid']                       = $vassetiValue->vasseti_id;
                                    $area['title']                       = null;
                                    $area['subtitle']                    = null;
                                    $area['img']                         = null;
                                    $area['tags']                        = null;
                                    $area['onfocus']['route']['tplType'] = null;
                                    $area['onfocus']['route']['tplID']   = null;
                                    $area['onfocus']['href']             = null;
                                    $area['onclick']['route']['tplType'] = null;
                                    $area['onclick']['route']['tplID']   = null;
                                    $area['onclick']['href']             = null;
                                }
                            } else if ($vassetiValue->type == 2) {
                                $vchan = DB::table('vchans')->where(['vchan_id' => $vassetiValue->vasseti_id])->first();
                                if (!empty($vchan)) {
                                    $area['rowid']    = $vchan->vchan_id;
                                    $area['title']    = $vchan->title;
                                    $area['subtitle'] = $vchan->subtitle;
                                    // 图片
                                    if ($templateAreaData_value->area_no == 'AREA005x1x0y0') {
                                        $templateImgs = DB::table('template_area_images')->where(['area_no' => $templateAreaData_value->area_no, 'subcolumn_id' => $subcolumn_id])->first();
                                        if ($templateImgs) {
                                            $area['img'] = json_decode(json_encode(['href' => $ftpUrl . $templateImgs->img]));
                                        } else {
                                            $area['img'] = (object) [];
                                        }
                                    } else {
                                        $img = $vchan->vlogo;
                                        if (empty($img)) {
                                            $area['img'] = (object) [];
                                        } else {
                                            $area['img'] = json_decode(json_encode(['href' => $ftpUrl . $vchan->vlogo]));
                                        }
                                    }
                                    $area['tags']                        = explode('&', $vchan->tags);
                                    $area['onfocus']['route']['tplType'] = null;
                                    $area['onfocus']['route']['tplID']   = null;
                                    $area['onfocus']['href']             = url("/{$spid}/{$termid}/vchan/{$vchan->vchan_id}/meta");
                                    $area['onclick']['route']['tplType'] = null;
                                    $area['onclick']['route']['tplID']   = null;
                                    $area['onclick']['href']             = url("/{$spid}/{$termid}/vchan/{$vchan->vchan_id}/meta");
                                } else {
                                    $area['rowid']                       = $vassetiValue->vasseti_id;
                                    $area['title']                       = null;
                                    $area['subtitle']                    = null;
                                    $area['img']                         = null;
                                    $area['tags']                        = null;
                                    $area['onfocus']['route']['tplType'] = null;
                                    $area['onfocus']['route']['tplID']   = null;
                                    $area['onfocus']['href']             = null;
                                    $area['onclick']['route']['tplType'] = null;
                                    $area['onclick']['route']['tplID']   = null;
                                    $area['onclick']['href']             = null;
                                }
                            }
                            $datas[] = $area;
                        }
                        if (count($datas) > 1) {
                            $areas[$templateAreaData_value->area_no]["type"] = "ul";
                        } else {
                            $areas[$templateAreaData_value->area_no]['type'] = "tag";
                        }
                        // $areas[$templateAreaData_value->area_no]['data'] = $datas;
                        $areas[$templateAreaData_value->area_no] = $datas;
                        $datas                                   = [];
                    }
                }
                $data['data']['data'] = $areas;
            }
        }

        $data['data']['route']['AREA005x1x0y0']['onleft']  = "boarder";
        $data['data']['route']['AREA005x1x0y0']['onright'] = "AREA005x1x1";
        $data['data']['route']['AREA005x1x0y0']['onup']    = "boarder";
        $data['data']['route']['AREA005x1x0y0']['ondown']  = "AREA005x1x0y1";

        $data['data']['route']['AREA005x1x0y1']['onleft']  = "boarder";
        $data['data']['route']['AREA005x1x0y1']['onright'] = "AREA005x1x1";
        $data['data']['route']['AREA005x1x0y1']['onup']    = "AREA005x1x0y0";
        $data['data']['route']['AREA005x1x0y1']['ondown']  = "boarder";

        $data['data']['route']['AREA005x1x1']['onleft']  = "AREA005x1x0y0";
        $data['data']['route']['AREA005x1x1']['onright'] = "boarder";
        $data['data']['route']['AREA005x1x1']['onup']    = "boarder";
        $data['data']['route']['AREA005x1x1']['ondown']  = "boarder";

        // 第一页6条,之后都是12条
        $total = DB::table('template_subcolumn_vasseti_datas')->where(['subcolumn_id' => $subcolumn_id, 'area_no' => "AREA005x1x1"])->count();
        // 第一页
        if ($total > 6) {
            $total = ceil(($total - 6) / 12);
            $total += 1;
        } else {
            $total = 1;
        }
        $data['data']['modelInfo']['page']['nowpage']   = $page;
        $data['data']['modelInfo']['page']['totalpage'] = $total;
        $limit                                          = 12;
        // 当前页大于或者等于时总页是,页数为当前页
        if ($page >= $total) {
            $rightPage = $total;
        } else {
            $rightPage = $page + 1;
        }
        $data['data']['modelInfo']['page']['rightpage'] = url("/{$spid}/{$termid}/column/{$collist_id}/list2/{$column_id}/{$subcolumn_id}?page={$rightPage}&limit={$limit}");
        // 当前页
        if ($page <= 1) {
            $leftPage = 1;
        } else {
            $leftPage = $page - 1;
        }
        // $page = $page>1 ? ($page-1):1;
        $data['data']['modelInfo']['page']['lefttpage'] = url("/{$spid}/{$termid}/column/{$collist_id}/list2/{$column_id}/{$subcolumn_id}?page={$leftPage}&limit={$limit}");

        return response()->json(['status' => '200', 'data' => $data]);
    }

    /**
     * 获取栏目的0-1级数据 获取栏目以及栏目下面的二级栏目列表
     * @get("{spid}/column/{columnid}/list1")
     * @Versions({"v1"})
     * @Request()
     * @Response("status": 200, "data":{},"route":{}})
     */
    public function list1Index(Request $request, $spid, $termid, $column_id)
    {
        // 栏目ID
        if (empty($column_id)) {
            return response()->json(['status' => '404', 'data' => '栏目不能为空!']);
        }
        if (empty($termid)) {
            return response()->json(['status' => '404', 'data' => '设备ID为空!']);
        }
        // 根据栏目ID去区位模板表查询所有的数据此栏目的数据
        $column = DB::table('columns')->where(['columnid' => $column_id])->first();
        if (empty($column)) {
            return response()->json(['status' => '404', 'data' => '栏目数据为空!']);
        }
        // FTP 配置
        $ftpUrl                              = config('app.host.ftp');
        $data['rowid']                       = $column->columnid;
        $data['img']                         = $ftpUrl . $column->img;
        $data['title']                       = $column->title;
        $data['subtitle']                    = $column->subtitle;
        $data['tags']                        = json_decode($column->tags);
        $data['onfocus']['route']['tplType'] = null;
        $data['onfocus']['route']['tplID']   = null;
        $data['onfocus']['href']             = url("/{$spid}/{$termid}/column/" . $column->columnid . "/list1");
        $data['onclick']['route']['tplType'] = null;
        $data['onclick']['route']['tplID']   = null;
        $data['onclick']['href']             = url("/{$spid}/{$termid}/column/" . $column->columnid . "/list1");
        // 根据栏目的ID获取下面的所有二级栏目列表
        $subcolumn = DB::table('subcolumns')->where(['columnid' => $column_id])->orderBy('listorder')->get();

        if ($subcolumn->first()) {
            foreach ($subcolumn as $key => $value) {
                // 二级栏目列表ID
                $data['data'][$key]['rowid']                       = $value->subcolumnid;
                $data['data'][$key]['img']                         = $ftpUrl . $value->img;
                $data['data'][$key]['title']                       = $value->title;
                $data['data'][$key]['subtitle']                    = $value->subtitle;
                $data['data'][$key]['onfocus']['route']['tplType'] = null;
                $data['data'][$key]['onfocus']['route']['tplID']   = null;
                $data['data'][$key]['onfocus']['href']             = url("/{$spid}/{$termid}/column/" . $column_id . "/list2/{$column->columnid}/{$value->subcolumnid}?page=1&limit=12");
                $data['data'][$key]['onclick']['route']['tplType'] = null;
                $data['data'][$key]['onclick']['route']['tplID']   = null;
                $data['data'][$key]['onclick']['href']             = url("/{$spid}/{$termid}/column/" . $column_id . "/list2/{$column->columnid}/{$value->subcolumnid}?page=1&limit=12");
            }
        } else {
            $data['data'] = [];
        }
        return response()->json(['status' => '200', 'data' => $data]);
    }

    /**
     * 获取二级栏目下媒资详情
     * @get(null)
     * @Versions({"v1"})
     * @Request()
     * @Response("status": 200, "data":{},"route":{}})
     */
    public function vassetiDetails(Request $request, $spid, $termid, $columnid, $subcolumnid)
    {
        if (empty($columnid)) {
            return response()->json(['status' => '404', 'data' => '栏目列表ID不能为空!']);
        }
        $subcolumns = DB::table('subcolumns')
            ->where(['columnid' => $columnid, 'subcolumnid' => $subcolumnid])
            ->first();
        if (empty($subcolumns)) {
            return response()->json(['status' => '404', 'data' => '没有找到此二级栏目!']);
        }
        // 取出栏目下面所有的媒资ID
        $vassetIds = explode('&', $subcolumns->vassetis);
        if (empty($vassetIds)) {
            return response()->json(['status' => '404', 'data' => '此栏目下没有关联媒资文件!']);
        }
        // FTP 配置
        $ftpUrl = config('app.host.ftp');
        $data   = [];
        $datas  = [];
        foreach ($vassetIds as $vasseti_key => $vasseti_value) {
            $results = DB::table('vassetis')->where(['vasseti_id' => $vasseti_value])->first();
            // 媒资ID
            $data['vasseti_id'] = $results->vasseti_id;
            // 标题
            $data['title'] = $results->title;
            // 副标题
            $data['subtitle'] = $results->subtitle;
            // 海报
            $posters = json_decode($results->posters, 1);
            if (empty($posters)) {
                $data['posters'] = (object) [];
            } else {
                $posters[0]['href'] = $ftpUrl . $posters[0]['href'];
                $data['posters']    = $posters[0];
            }

            // 图片
            $img = json_decode($results->imgs, 1);
            if (empty($img)) {
                $data['img'] = (object) [];
            } else {
                foreach ($img as $imgKey => $imgValue) {
                    $imgHref = image_match($imgValue['href'], 'SIMG');
                    if ($imgHref) {
                        $data['img'] = $ftpUrl . $imgHref;
                        break;
                    } else {
                        $data['img'] = (object) [];
                    }
                }
            }
            // 获取焦点事件
            $data['onclick']['route']["tplType"] = null;
            $data['onclick']['route']["tplID"]   = null;
            $data['onclick']['href']             = url("/{$spid}/{$termid}/vasset/{$vasseti_value}/meta");
            $datas[]                             = $data;
        }
        return response()->json(['status' => '200', 'data' => $datas]);
    }

    /**
     * 获取指定服务商指定 栏目信息
     * @get("{spid}/column/{columnid}/list0")
     * @Versions({"v1"})
     * @Request()
     * @Response("status": 200, "data":{},"route":{}})
     */
    public function metaIndex(Request $request, $spid, $termid, $columnid)
    {

        // 栏目商ID不能为空
        if (empty($columnid)) {
            return response()->json(['status' => '200', 'data' => '栏目ID不能为空!']);
        }
        // 设备ID不能为空
        if (empty($termid)) {
            return response()->json(['status' => '404', 'data' => '设备ID为空!']);
        }
        $columns = DB::table('columns')->where('columnid', $columnid)->get();
        $data    = [];
        $datas   = [];

        if (empty($columns)) {
            return response()->json(['status' => '404', 'data' => '没有关联栏目信息!']);
        }
        // FTP 配置
        $ftpUrl = config('app.host.ftp');
        foreach ($columns as $key => $value) {
            $data['rowid']    = $value->columnid;
            $data['img']      = $ftpUrl . $value->img;
            $data['title']    = $value->title;
            $data['subtitle'] = $value->subtitle;
            $data['tags']     = json_decode($value->tags);
            $collistid        = json_decode($value->collistid, 1)[0];

            $data['onfocus']['route']['tplType'] = null;
            $data['onfocus']['route']['tplID']   = null;
            $data['onclick']['route']['tplType'] = null;
            $data['onclick']['route']['tplID']   = null;
            $data['onfocus']['href']             = url("{$spid}/{$termid}/collist/{$collistid}/columns/" . ($value->columnid));
            $data['onclick']['href']             = url("{$spid}/{$termid}/collist/{$collistid}/columns/" . ($value->columnid));
            $datas[]                             = $data;
        }
        return response()->json(['status' => '200', 'data' => $datas]);
    }


    /**
     * 获取栏目的所有数据
     * @get("{spid}/column/{columnid}/list")
     * @Versions({"v1"})
     * @Request()
     * @Response("status": 200, "data":{},"route":{}})
     */
    public function listAllSubcolumn(Request $request, $spid, $column_id)
    {
        // $termid = $request->header('device-token');
        // 栏目ID
        if (empty($column_id)) {
            return response()->json(['status' => '404', 'data' => '栏目不能为空!']);
        }
        // 根据栏目ID去区位模板表查询所有的数据此栏目的数据
        $subcolumnIds = DB::table('template_area_datas')->where(['column_id' => $columnid])->pluck('subcolumn_id');
        $subcolumnIds = array_unique($subcolumnIds);
        //  栏目下面所有的栏目媒资信息
        $colums = [];
        $data   = [];
        $datas  = [];
        $areas  = [];
        // 每一个栏目都去遍历相应的数据
        foreach ($subcolumnIds as $subcolumnId_key => $subcolumnId_value) {
            // $areaVassteiData = DB::table('template_subcolumn_vasseti_datas')->where('area_no','AREA005x1x0y0')->pluck('vasseti_id');
            // $areaVassteiData['AREA005x1x0y0']
            // 每一个栏目下面都有三部分信息
            $areaNo = DB::table('template_area_datas')->where(['subcolumn_id' => $subcolumnId_value])->groupBy('area_no')->get();

            if (!empty($areaNo)) {
                foreach ($areaNo as $areaNoKey => $areaNoValue) {
                    // 根据每个编号取出相关的媒资Id
                    $vassetiIds = DB::table('template_subcolumn_vasseti_datas')->where(['subcolumn_id' => $subcolumnId_value])->pluck('vasseti_id');
                    foreach ($vassetIds as $vassetId_key => $vassetId_value) {
                        $vassetis = DB::table('vassetis')->where(['vasset_id' => $vassetId_value])->first();
                        if (!empty($vassetis)) {
                            $data['rowid']                       = $vassetis->vasseti_id;
                            $data['img']                         = json_decode($vassetis->imgs, 1);
                            $data['title']                       = $vassetis->title;
                            $data['subtitle']                    = $vassetis->subtitle;
                            $data['short_desc']                  = $vassetis->short_desc;
                            $data['tags']                        = DB::table('vasseti_vcattags')->where('vasseti_vcattags.vasseti_id', $vassetis->vasseti_id)->join('vcattags', 'vasseti_vcattags.vcattag_id', '=', 'vcattags.vcattag_id')->pluck('vcattags.name')->implode(',', 'name');
                            $data['onfocus']['route']['tplType'] = null;
                            $data['onfocus']['route']['tplID']   = null;
                            $data['onfocus']['href']             = url("/{$spid}/vasset/{$vassetis->vasseti_id}/meta");
                            $data['onclick']['route']['tplType'] = null;
                            $data['onclick']['route']['tplID']   = null;
                            $data['onclick']['href']             = url("/{$spid}/vasset/{$vassetis->vasseti_id}/meta");
                            $datas                               = [];
                        }
                    }
                    $areas[$areaNoValue] = $datas;
                    $datas               = [];
                }
            }
            $colums[] = $areas;
            $areas    = [];
        }
        return response()->json(['status' => '200', 'data' => $areas]);
    }
}
